//
//  ApproveOptionsAlert.swift
//  GoCash
//
//  Created by jie on 2024/5/16.
//

import SwiftUI

struct OptionModel: Identifiable {
    var title: String = ""
    var value: Int = 0
    var isSel: Bool = false
    
    var id: Int { return value }
}


struct ApproveOptionsAlert: View {
    
    @State var options: [OptionModel] = []
    
    var optionSelectedCompletion: (OptionModel)->Void
    
    var body: some View {
        ZStack {
            Color.white.cornerRadius(16)
            VStack(spacing: 0) {
                HStack{
                    Text("Tipo de empleo").font(.system(size: 18, weight: .medium)).foregroundColor(COLOR_312A31).frame(maxWidth: .infinity)
                    Spacer()
                    Button(action: {
                        SwiftMessageManager.hidden()
                    }, label: {
                        Image(systemName: "xmark").foregroundColor(COLOR_312A31)
                    })
                }.padding(.horizontal, 16).padding(.top, 25)
                Spacer().frame(height: 20)
                ScrollView(showsIndicators: false) {
                    ForEach(0..<options.count, id: \.self) { i in
                        ApproveOptionItem(option: $options[i]) { option in
                            updateOptions(option)
                            DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
                                SwiftMessageManager.hidden()
                                optionSelectedCompletion(option)
                            }
                        }
                    }
                }
                .padding(.horizontal, 16)
                .padding(.bottom, 16)
                
                Spacer()
            }
        }.edgesIgnoringSafeArea(.bottom).frame(maxHeight: SCREEN_HEIGHT * 0.7).padding(0)
    }
    
    func updateOptions(_ option: OptionModel) {
        for i in 0..<options.count {
            if options[i].value == option.value {
                options[i].isSel = true
            } else {
                options[i].isSel = false
            }
        }
    }
}


#Preview(body: {
    ApproveOptionsAlert(options: [
        OptionModel(value: 1, isSel: true),
        OptionModel(value: 2),
        OptionModel(value: 3),
        OptionModel(value: 4),
        OptionModel(value: 5),
        OptionModel(value: 6),
    ]) { option in
        
    }
})



struct ApproveOptionItem: View {
    
    @Binding var option: OptionModel
    var onTap: (OptionModel)->Void
    
    var body: some View {
        HStack{
            Text("A tiempo completo").font(.system(size: 16)).foregroundColor(COLOR_312A31)
            Spacer()
            Image(systemName: "circle").foregroundColor(statusColor())
        }
        .padding()
        .frame(height: 54)
        .frame(maxWidth: .infinity)
        .padding(.horizontal, 16)
        .overlay(
            RoundedRectangle(cornerRadius: 8, style: .continuous)
                .stroke(statusColor(), lineWidth: 1).padding(0)
        )
        .contentShape(Rectangle())
        .onTapGesture {
            onTap(option)
        }
    }
    
    func statusColor() -> Color {
        return option.isSel==true ? MAIN_COLOR : COLOR_EFEBF4
    }
}


//struct ApproveOptionsAlert: View {
//    @State private var isBottomSheetPresented = true
//
//    var body: some View {
//        VStack {
//            Text("主内容")
//                .padding()
//                .onTapGesture {
//                    isBottomSheetPresented.toggle()
//                }
//        }
//        .bottomSheet(isPresented: $isBottomSheetPresented) {
//            AnyView(
//                VStack {
//                    Rectangle().foregroundColor(.green).frame(height: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/)
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                    Text("123243")
//                })
//        }
//    }
//}
//
//#Preview {
//    ApproveOptionsAlert()
//}
//
//
//
//struct BottomSheetModifier: ViewModifier{
//    @Binding var isPresented: Bool
//    
//    @Binding var customerContent: AnyView
//
//    func body(content: Content) -> some View {
//        GeometryReader(content: { geometry in
//            ZStack {
//                VStack {
//                    Spacer()
////                    content
//                    customerContent
//                        .background(.gray)
//                        .cornerRadius(16)
//                        .overlay(
//                            RoundedRectangle(cornerRadius: 16)
//                                .stroke(Color.primary, lineWidth: 1)
//                        )
//                }
//            }.animation(.default, value: isPresented)
//                .edgesIgnoringSafeArea(.bottom)
//                .opacity(isPresented ? 1 : 0)
//        })
//        .background(Color.black.opacity(0.3)
//        .edgesIgnoringSafeArea(.all))
//    }
//}
//
//extension View {
//    func bottomSheet(isPresented: Binding<Bool>, content: ()->AnyView) -> some View {
//        self.modifier(
//            BottomSheetModifier(
//                isPresented: isPresented,
//                customerContent: .constant(content())
//            )
//        )
//    }
//}
